//
//  ROFoodListTableViewController.swift
//  neufood
//
//  Created by huangminmin on 2017/10/24.
//  Copyright © 2017年 huangminmin. All rights reserved.
//

import UIKit

class ROFoodListTableViewController: UITableViewController {
    var shop: ROShopModel!{ //商店模型
        didSet(oldValue){
            shopId = shop.shop_id
            shopName = shop.shopname
        }
    }
    
    var shopId: NSNumber!
    var shopName: NSString!
    var foodList: NSArray = NSArray()
    var collectionBarBtnItem: UIBarButtonItem!//右侧按钮
    var isCollect : NSString!//查看是否收藏
    var rightBtn: UIButton = UIButton.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.isNavigationBarHidden = false
        //导航右按钮，收藏店铺
        rightBtn.sizeToFit()
        rightBtn.addTarget(self, action:#selector(collectionShop), for: UIControlEvents.touchUpInside)
        collectionBarBtnItem = UIBarButtonItem.init(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = collectionBarBtnItem
        self.title = self.shopName as String?
        
        let parameters: NSDictionary = ["shop_id": shopId] as NSDictionary
        RONetworkMngTool.sharedNetWorkMngTool().RONetwork_GetFoodListParameters(parameters, view: self.view, block: { (foodList) -> Void in
            self.foodList = foodList
            self.tableView.reloadData()
        })
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    override func viewWillAppear(_ animated: Bool) {
        
        isCollected()
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if self.foodList.count == 0 {
            return 0
        }
        return self.foodList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "foodCell", for: indexPath)
        if self.foodList.count != 0 {
            let food: ROFoodModel = self.foodList[(indexPath as NSIndexPath).row] as! ROFoodModel
            cell.textLabel!.text = food.foodname as String
            let imagestr = food.pic as String
            let imageUrl = URL(string: imagestr)
            cell.imageView!.setImageWith(imageUrl, placeholderImage: UIImage(named: "placeHolder"))
            cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        }
        
        
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let currentFood: ROFoodModel = foodList[(indexPath as NSIndexPath).row] as! ROFoodModel
        self.performSegue(withIdentifier: "tofoodDetail", sender: currentFood)
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let foodDetailController: ROFoodDetailTableViewController = segue.destination as! ROFoodDetailTableViewController
        foodDetailController.food = sender as! ROFoodModel
    }
    func collectionShop (){
        //下面方法完成两个功能，如果之前已经收藏，那么次函数的功能就是取消收藏；如果之前没有收藏，那么此函数的功能就是添加收藏
        let parameters2: NSDictionary = ["user_id": userId,"shop_id": self.shopId] as NSDictionary
        RONetworkMngTool.sharedNetWorkMngTool().RONetwork_collectShopWithParameters(parameters2, view: self.view) { (success) in
            if success == "1" {
                self.isCollected()
            } else {
            }
        }
    }
    //判断之前是否收藏
    func isCollected() {
        
        let parameters: NSDictionary = ["user_id": userId,"shop_food_id": self.shopId,"flag": 0] as NSDictionary
        RONetworkMngTool.sharedNetWorkMngTool().RONetwork_isCollectedWithParameters(parameters, view: self.view) { (isCollect) in
            self.isCollect = isCollect
            
            if self.isCollect == "0" {
                self.rightBtn.setImage(UIImage.init(named: "unlike.png"), for: UIControlState.normal)
            }else if self.isCollect == "1" {
                self.rightBtn.setImage(UIImage.init(named: "like.png"), for: UIControlState.normal)
            }
            
        }
    }
    
    /*
     // Override to support conditional editing of the table view.
     override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
     // Return false if you do not want the specified item to be editable.
     return true
     }
     */
    
    /*
     // Override to support editing the table view.
     override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
     if editingStyle == .delete {
     // Delete the row from the data source
     tableView.deleteRows(at: [indexPath], with: .fade)
     } else if editingStyle == .insert {
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
     }
     }
     */
    
    /*
     // Override to support rearranging the table view.
     override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
     
     }
     */
    
    /*
     // Override to support conditional rearranging of the table view.
     override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
     // Return false if you do not want the item to be re-orderable.
     return true
     }
     */
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
